﻿// ButtonControl.cpp : 定义应用程序的入口点。
//

#include "framework.h"
#include "ButtonControl.h"

#define MAX_LOADSTRING 100

// 全局变量:


// 此代码模块中包含的函数的前向声明:
VOID EditPrintf(LPCTSTR szFormat, ...);
INT_PTR CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此处放置代码。
    HWND hWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc);
    ShowWindow(hWnd, SW_SHOW);

    MSG msg;

    // 主消息循环:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

HWND hwndConn;// 连接服务器按钮
HWND hwndDisconn;// 断开服务器按钮
HWND hwndEdit;// 显示用的文本框


// “关于”框的消息处理程序。
INT_PTR CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);

    switch (message)
    {
    case WM_INITDIALOG:
        hwndConn = GetDlgItem(hDlg, IDC_CONN);
        hwndDisconn = GetDlgItem(hDlg, IDC_DISCONN);
        hwndEdit = GetDlgItem(hDlg, IDC_EDIT_OUT);

        return (INT_PTR)TRUE;

    case WM_COMMAND:
        {
            switch (LOWORD(wParam))
            {
            case IDC_CONN:
                //ShowWindow(hwndConn, SW_HIDE);// 隐藏“连接服务器”按钮
                //ShowWindow(hwndDisconn, SW_SHOWNORMAL);// 显示“断开服务器”按钮

                EnableWindow(hwndConn, FALSE);// 停用“连接服务器”按钮
                EnableWindow(hwndDisconn, TRUE);// 启用“断开服务器”按钮
                EditPrintf(TEXT("连接服务器成功 %i...\r\n"), IDC_CONN);
                return (INT_PTR)TRUE;
            case IDC_DISCONN:
                //ShowWindow(hwndConn, SW_SHOWNORMAL);// 显示“连接服务器”按钮
                //ShowWindow(hwndDisconn, SW_HIDE);// 隐藏“断开服务器”按钮

                EnableWindow(hwndConn, TRUE);// 启用“连接服务器”按钮
                EnableWindow(hwndDisconn, FALSE);// 停用“断开服务器”按钮
                EditPrintf(TEXT("断开服务器成功 %i...\r\n"), IDC_DISCONN);
                return (INT_PTR)TRUE;
            }
        }
        break;
    case WM_CLOSE:
        PostQuitMessage(0);
        break;
    }
    return (INT_PTR)FALSE;
}

VOID EditPrintf(LPCTSTR szFormat, ...) {
    TCHAR szBuffer[1024];
    va_list pList;

    va_start(pList, szFormat);
    wvsprintf(szBuffer, szFormat, pList);
    va_end(pList);

    SendMessage(hwndEdit, EM_SETSEL, (WPARAM)-1, (LPARAM)-1);
    SendMessage(hwndEdit, EM_REPLACESEL, FALSE, (LPARAM)szBuffer);
    SendMessage(hwndEdit, EM_SCROLLCARET, 0, 0);
}
